package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class BorrowBook implements IOPeration{
    @Override
    public void work(BookList bookList){
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入你要借阅的图书书号:");
        String id0 = scan.next();
        //用于记录借阅图书的索引位置
        int num = -1;
        for (int i = 0; i < bookList.books.size(); i++) {
            //检查是否有该书号的书
            Book Book = bookList.books.get(i);
            if (Book.id.equals(id0)) {
                num = i;
                System.out.println(Book);
            }
        }
        if(num == -1){
            System.out.println("未找到该书号的书");
            return;
        }
        if(bookList.books.get(num).num <= 0){
            System.out.println("库存不足,借阅失败");
        }else{
            Book Book = bookList.books.get(num);
            //为书名后添加标记
            Book.setName(Book.getName() + "(已借)");
            //减少相应的库存
            Book.setNum(Book.getNum() - 1);
            bookList.books.set(num,Book);
            System.out.println("借阅成功");
        }
    }
}
